window.ROOT = "";
(function () {
    var serverData = {};
    var	serverTable;
    jQuery(document).ready(function () {

        serverTable=  jQuery('#dataTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url": window.rootPath+'/ui/role/getpage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [{
                data: null,
                defaultContent: ""
            },
                {
                    data: 'name'
                },
                {
                    data: 'description'
                },
                {
                    data:null,
                    defaultContent: "",
                    render: function(data, type, full, meta){
                        var operate ='<button type="button" class="btn btn-sm btn-info setBtn" title="配置模型"><i class="fa fa-link"></i></button>';

                        return operate;

                    }
                }],
            "drawCallback": function ( oSettings ) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for ( var i=oSettings._iDisplayStart, iLen=total ; i<iLen ; i++ )
                {
                    $('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[k] ].nTr ).text( i+1 );
                    k++;
                }

                serverTable.off('click','tr');
                serverTable.on( 'click', 'tr', function (event) {
                    event=event||window.event;
                    var target=event.target||event.scrElement;
                    var data = serverTable.row( this ).data();

                    if(target.className.indexOf('fa-link')!=-1||target.className.indexOf('setBtn')!=-1){

                        setModule(data);
                    }

                    return;
                } );
            }
        });
        //查询
        jQuery('.searchBtn').on('click',function(){
            serverData.key=jQuery('#keyword').val();
            serverTable.draw();
        })
        //撤销查询
        jQuery('.cancelBtn').on("click",function()
        {
            jQuery('#keyword').val('');
            serverData.key ='';
            serverTable.draw();
        });
		var setModule = function(data){
		var roleId=data.id;
		    var url=window.rootPath+'/ui/m2r/moduleSet'+'?roleId='+roleId;
		    window.location=url;
		}

    });
})();
